﻿using DapperExtensions;
using KD.Service.HR.Utility;
using Logistics.Model;
using Logistics.Model.QueryModel;
using Logistics.Service;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace Logistics.WebApi.Controllers
{
    [MyAuthority]
    public class ContractController : System.Web.Http.ApiController
    {
        private ContractService service = new ContractService();
        private ContractCancelService ccService = new ContractCancelService();
        private CompanyService companyService = new CompanyService();
        [HttpPost]
        /// <summary>
        /// 获取签约管理列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResponseModel GetList(ContractQuery query)
        {
            var responseModel = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                int company_id = companyService.GetCompanyIdByAccountId(accountId);
                var list = new List<IPredicate>();
                if (!string.IsNullOrEmpty(query.companyname))
                    list.Add(Predicates.Field<wuliu_contract_base>(f => f.wuliu_name, Operator.Eq, query.companyname));
                if (!string.IsNullOrEmpty(query.cityid))
                    list.Add(Predicates.Field<wuliu_contract_base>(f => f.wuliu_city_id, Operator.Like, query.cityid));
                list.Add(Predicates.Field<wuliu_contract_base>(f => f.contract_flag, Operator.Eq, 1));
                list.Add(Predicates.Field<wuliu_contract_base>(f => f.contract_state, Operator.Eq, 10));
                list.Add(Predicates.Field<wuliu_contract_base>(f => f.company_id, Operator.Eq, company_id));
                var pg = Predicates.Group(GroupOperator.And, list.ToArray());
                responseModel.mod = service.GetContractList(pg, query.page - 1, query.pagesize);
                responseModel.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }
        [HttpPost]
        /// <summary>
        /// 解约
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel Cancel(wuliu_contract_cancel_query cancel)
        {
            var responseModel = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                //var id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
                var result = service.CancelContract(cancel.id);

                //var contract= service.GetContract(id);
                //往解约表里面插数据
                cancel.apply_flag = "10";
                cancel.Operator = accountId.ToString();
                var cancelId = ccService.Add(cancel);

                responseModel.mod = result && cancelId > 0;
                if (result)
                    responseModel.InitResultState(GetDataStateEnum.Success);
                else
                    responseModel.InitResultState(GetDataStateEnum.Error);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }
    }
}